package com.javasee.common.dbsharding;

import org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource;

import java.util.Objects;

/**
 * 数据源切换
 */
public class DbShardingRoutingDataSource extends AbstractRoutingDataSource {

    @Override
    protected Object determineCurrentLookupKey() {
        Integer dbIndex = -1;
        if (Objects.isNull(DbShardingContextHolder.get())){
            dbIndex = 0;
        }else {
            dbIndex = DbShardingContextHolder.get().getDbIndex();
        }
        return "db" + (dbIndex + 1);
    }
}
